﻿using LightingPlatform.ViewModel;
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace LightingPlatform
{
    public interface IPageProvider
    {
        PageLevel PageLevel { get; }
        Frame Navigation { get; set; }
    }

    public enum RoleType
    {
        Normal = 1,
        Advanced = 2,
        DB = 3,
        System = 4,
    }

    public enum PageLevel
    {
        First,
        Second,
        Third,
        Fourth,
        Fifth
    }

    public abstract class DisplayControler : IValueConverter
    {
        private PageLevel _pageLevel;

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            _pageLevel = (PageLevel)value;
            return IsVisible ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        protected virtual bool IsVisible
        {
            get
            {
                return BusinessContext.Singleton.User != null;
            }
        }

        protected RoleType TargetRoleType
        {
            get
            {
                return (RoleType)Enum.Parse(typeof(RoleType), BusinessContext.Singleton.User.RoleType.ToString(), true);
            }
        }

        protected PageLevel TargetPage
        {
            get
            {
                return _pageLevel;
            }
        }
    }

    public class LoginConverter : DisplayControler
    {
        protected override bool IsVisible
        {
            get
            {
                return BusinessContext.Singleton.User == null;
            }
        }
    }

    public class UserConverter : DisplayControler
    {
        protected override bool IsVisible
        {
            get
            {
                if (base.IsVisible)
                    return TargetRoleType == RoleType.System;
                return false;
            }
        }
    }

    public class ImportConverter : DisplayControler
    {
        protected override bool IsVisible
        {
            get
            {
                if (base.IsVisible)
                    return TargetRoleType == RoleType.System || TargetRoleType == RoleType.DB;
                return false;
            }
        }
    }

    public class EditConverter : ImportConverter
    {
        protected override bool IsVisible
        {
            get
            {
                if (base.IsVisible)
                    return TargetPage == PageLevel.Fourth;
                return false;
            }
        }
    }

    public class DeleteConverter : ImportConverter
    {
        protected override bool IsVisible
        {
            get
            {
                if (base.IsVisible)
                    return TargetPage == PageLevel.Fourth;
                return false;
            }
        }
    }

    public class PrintConverter : DisplayControler
    {
        protected override bool IsVisible
        {
            get
            {
                if (base.IsVisible)
                {
                    return TargetPage == PageLevel.Fourth
                        || TargetPage == PageLevel.Third
                        || TargetPage == PageLevel.Fifth;
                }
                return false;
            }
        }
    }
}
